/*
*  Copyright 2019-2020 Liu
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.rest;


import com.liu.annotation.AnonymousAccess;
import com.liu.modules.business.domain.NovelDetailInfo;
import com.liu.modules.business.repository.NovelDetailInfoRepository;
import com.liu.modules.business.service.NovelDetailInfoService;
import com.liu.modules.business.service.dto.NovelDetailInfoQueryCriteria;
import com.liu.annotation.Log;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Liu
* @date 2022-07-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "章节管理管理")
@RequestMapping("/api/novelDetailInfo")
public class NovelDetailInfoController {

    private final NovelDetailInfoService novelDetailInfoService;
    private final NovelDetailInfoRepository novelDetailInfoRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('novelDetailInfo:list')")
    public void download(HttpServletResponse response, NovelDetailInfoQueryCriteria criteria) throws IOException {
        novelDetailInfoService.download(novelDetailInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询章节管理")
    @ApiOperation("查询章节管理")
    @AnonymousAccess
    public ResponseEntity<Object> query(NovelDetailInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(novelDetailInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增章节管理")
    @ApiOperation("新增章节管理")
    @PreAuthorize("@el.check('novelDetailInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody NovelDetailInfo resources){
        return new ResponseEntity<>(novelDetailInfoService.create(resources),HttpStatus.CREATED);
    }

    @PostMapping(value = "/upload")
    @Log("上传章节管理")
    @ApiOperation("上传章节")
    @AnonymousAccess
    public ResponseEntity<Object> create(@RequestParam String novelId,String chapterTitle, @RequestParam("file") MultipartFile file){
        return new ResponseEntity<>(novelDetailInfoService.upload(novelId,chapterTitle,file),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改章节管理")
    @ApiOperation("修改章节管理")
    @PreAuthorize("@el.check('novelDetailInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody NovelDetailInfo resources){
        novelDetailInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除章节管理")
    @ApiOperation("删除章节管理")
    @PreAuthorize("@el.check('novelDetailInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        novelDetailInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("查询上一章")
    @ApiOperation("查询上一章")
    @GetMapping(value = "/getPrevNovelDetailInfo")
    @AnonymousAccess
    public ResponseEntity<Object> getPrevNovelDetailInfo(NovelDetailInfoQueryCriteria criteria) {
            return new ResponseEntity<>(novelDetailInfoService.getPrevNovelDetailInfo(criteria),HttpStatus.OK);
    }

    @Log("查询下一章")
    @ApiOperation("查询下一章")
    @GetMapping(value = "/getNextNovelDetailInfo")
    @AnonymousAccess
    public ResponseEntity<Object> getNextNovelDetailInfo(NovelDetailInfoQueryCriteria criteria) {
         return new ResponseEntity<>(novelDetailInfoService.getNextNovelDetailInfo(criteria),HttpStatus.OK);
    }

}